<?php

/**
 * 
 * @author 陈德本
 *
 */
class productController extends Controller {

    public $initphp_list = array(
        'index',
        'details'
    );
    private $conf;
    public function __construct(){
        parent::__construct();
        $this->conf=InitPHP::getConfig();
        $this->view->assign("pro_cat", $this->getProduct()->getMenu());
        $this->view->assign("nav", $this->getNav()->getNav(tree,1));
        $this->view->assign("slide", $this->getSlide()->getAll());
        $this->view->assign("link", $this->getLink());
        $this->view->assign("category", $this->getCatgory()->getCatgory());
    }
    public function index() {
        $id=$this->controller->get_get('id');
        $id=$id?:1;
        $cat_data=$this->getProduct()->getOneCat($id);
        $url="/".$cat_data['url'];
        $cat=$this->getProduct()->getCatTree2(false);
        $this->view->assign("cat", $cat);
        $this->view->assign("title", $cat_data['seo_title']);
        $pager = $this->getLibrary('pager');
        $page = $this->controller->get_gp("page");
        $page=$page>0?$page:1;
        $url_str= $_SERVER['REQUEST_URI'];
        if (strpos($url_str, "?")){
            Header("HTTP/1.1 301 Moved Permanently");
            $location=$page==1?$url:($url."/page".$page.".html");
            Header("Location: ".$location);
            die();
        }
        $pernum = 9;
        $start = $pernum * ($page - 1);
        $_data=$this->getProduct()->getList($id, $start, $pernum);
        $page_html = $pager->pager($_data[1], $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
        $this->view->assign("pager", $page_html);
        $this->view->assign("cat_data", $cat_data);
        $this->view->assign("pro_data", $_data[0]);
        $this->view->assign("title", $cat_data['seo_title']);
        $this->view->assign("keywords", $cat_data['keyword']);
        $this->view->assign("description", $cat_data['desc']);
        $this->view->set_tpl("home/product/list"); // 中间的模板
        $this->view->display();
    }
    public function details(){
        $id=$this->controller->get_get('id');
        $id=$id?:1;
        $data=$this->getProduct()->getOne($id);
        $cat_data=$this->getProduct()->getOneCat($data['pid']);
        if (!$data){
            JsMessage("产品不存在！","/home");
            die();
        }
        if ($data['has_attr']){
            $attr=$this->getProduct()->getAttr($id);
            $this->view->assign("attr", $attr);
        }
        $url_str= $_SERVER['REQUEST_URI'];
        if (strpos($url_str, "?")){
            Header("HTTP/1.1 301 Moved Permanently");
            Header("Location: /".$cat_data['url']."/".$data['id'].".html");
            die();
        }
        $this->view->assign("cat", $cat_data);
        $this->view->assign("title", $data['seo_title']?:$data['title']);
        $this->view->assign("data", $data);
        $this->view->assign("keywords", $cat_data['keyword']);
        $this->view->assign("description", $data['desc']);
        $this->view->set_tpl("home/product/content");
        $this->view->display();
    }

    /**
     *
     * @return navService
     */
    private function getNav(){
        return InitPHP::getService("nav");
    }
    
    /**
     * 
     * @return catgoryService
     */
    private function getCatgory(){
        return InitPHP::getService("catgory");
    }
    /**
     *
     * @return productService
     */
    private function getProduct(){
        return InitPHP::getService("product");
    }
    /**
     *
     * @return slideService
     */
    private function getSlide(){
        return InitPHP::getService("slide");
    }
    private function getLink(){
        return InitPHP::getDao("link")->getAll();
    }
}